﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace NKD07
{
    public partial class category : System.Web.UI.Page
    {
        // current page of paging of pagedatasource
        public int currentPage
        {
            get 
            {
                object obj = this.ViewState["currentPage"];
                if (obj == null)
                    return 0;
                else
                    return (int)obj; 
            }
            set 
            {
                this.ViewState["currentPage"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            // load all products depend on id of specific category 
            if (!IsPostBack)
            {
                LoadProductsByCategoryID(0);
            }
        }


        private void LoadProductsByCategoryID(int option)
        {
            Classes.product product = new Classes.product();
            Classes.category category = new Classes.category();
            try
            {
                string categoryID = Request.QueryString["categoryID"];
                string categoryName = category.GetCategoryNameByID(int.Parse(categoryID));
                lbCategoryName.Text = categoryName;
                if (categoryID != null)
                {
                    DataTable products =  product.GetProductsByCategoryID(int.Parse(categoryID), option);
                    //--ititialize paging for datalist
                    PagedDataSource pageDS = new PagedDataSource();
                    pageDS.DataSource = products.DefaultView;
                    pageDS.AllowPaging = true;
                    pageDS.PageSize = 4;
                    pageDS.CurrentPageIndex = currentPage;
                    if (pageDS.IsFirstPage)
                    {
                        lbtnPrev.Enabled = false;
                        lbtnPrev.Style["color"] = "";
                    }
                    else
                    {
                        lbtnPrev.Enabled = true;
                    }
                    if (pageDS.IsLastPage)
                    {
                        lbtnNext.Enabled = false;
                        lbtnNext.Style["color"] = "";
                    }
                    else
                    {
                        lbtnNext.Enabled = true;
                    }
                    //--end initialize paging for datalist

                    dtlProducts.DataSource = pageDS;
                    dtlProducts.DataBind();
                }
            }
            catch (Exception)
            {
                Response.Redirect("default.aspx");
            }
        }

        protected void lbtnPrev_Click(object sender, EventArgs e)
        {
            currentPage -= 1;
            string option = ddlSortValue.SelectedValue;
            LoadProductsByCategoryID(int.Parse(option));
        }

        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            currentPage += 1;
            string option = ddlSortValue.SelectedValue;
            LoadProductsByCategoryID(int.Parse(option));
        }

        protected void ddlSortValue_SelectedIndexChanged(object sender, EventArgs e)
        {
            currentPage = 0;
            string option = ddlSortValue.SelectedValue;
            LoadProductsByCategoryID(int.Parse(option));
        }


    }
}